package customer

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server/app/api/util"
	"sieve_admin_server/app/contextx"
	"sieve_admin_server/app/schema"
	"sieve_admin_server/app/server"
	serviceCustomer "sieve_admin_server/app/service/customer"
)

var (
	_                util.API = (*BusinessPriceAPI)(nil)
	BusinessPriceSet          = wire.NewSet(wire.Struct(new(BusinessPriceAPI), "*"))
)

type BusinessPriceAPI struct {
	util.EmptyAPI
	BusinessPriceSrv *serviceCustomer.BusinessPriceSrv
}

func (a *BusinessPriceAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "businessPrice",
			Title: "业务方案配置", Perms: []*server.Perm{
				{Name: "query", Title: "搜索"},
				{Name: "queryDetail", Title: "搜索"},
				{Name: "add", Title: "添加"},
				{Name: "del", Title: "删除"},
				{Name: "edit", Title: "编辑"},
				{Name: "export", Title: "导出"},
			},
			Order: 5,
		},
	}
}

func (a *BusinessPriceAPI) Cron() {

	_ = a.BusinessPriceSrv.Cron()
}

/*
	 prices 的格式:
		[{"name":"filter","unit_price":0.006},{"name":"gender","unit_price":0.006}]
*/
func (a *BusinessPriceAPI) API_add_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("name"),
				contextx.NewVar("description"),
				contextx.NewVar("prices"),
				contextx.NewVar("def", true),
			},
		)
	)

	return a.BusinessPriceSrv.Create(context.Background(), ctx.Uid, reqArgs)
}

func (a *BusinessPriceAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("name"),
				contextx.NewVar("description"),
				contextx.NewVar("def", true),
				contextx.NewVar("with_def", true), // 列表结果是否带上默认配置
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)

	return a.BusinessPriceSrv.Query(context.Background(), ctx.Uid, reqArgs, p)
}

func (a *BusinessPriceAPI) API_queryDetail_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("def", true),
			},
		)
	)

	return a.BusinessPriceSrv.QueryDetail(context.Background(), ctx.Uid, reqArgs)
}

/*
	 prices 的格式:
		[{"name":"filter","unit_price":0.006},{"name":"gender","unit_price":0.006}]
*/
func (a *BusinessPriceAPI) API_edit_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("name"),
				contextx.NewVar("description"),
				contextx.NewVar("prices"), // prices json格式
			},
		)
	)

	return a.BusinessPriceSrv.Update(context.Background(), ctx.Uid, reqArgs)
}

/*
删除的同时需要指定一个其他的业务方案
*/
func (a *BusinessPriceAPI) API_del_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("new_id", true),
			},
		)
	)

	return a.BusinessPriceSrv.Delete(context.Background(), uint64(ctx.Uid), reqArgs)
}

func (a *BusinessPriceAPI) API_export_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("ids"),
			},
		)
	)

	return a.BusinessPriceSrv.Export(context.Background(), ctx.Uid, reqArgs)
}
